<extend name="Base/common"/>
<block name="body">
	<link rel="stylesheet" type="text/css" href="__CSS__/sweet-alert.css">
	<link rel="stylesheet" type="text/css" href="__CSS__/redbag.css">
	<ul class="couten"></ul>
	<div class="sure_code">
		<img src="__IMG__/gx.png">
		<div class="info">获得幸运数字</div>
		<div class="code"></div>
		<div class="btns mui-row">
			<li class="mui-col-xs-6 btn_cancel">取消</li>
			<li class="mui-col-xs-6 btn_sure">确定</li>
			<input type="hidden" id="input_code_id">
		</div>
	</div>
	<div class="backward">
		<span></span>
	</div>	
</block>
<block name="script">
<script type="text/javascript">
	$(document).ready(function() {
		var snatch_id = '{$snatch_id}';
		var btn_lock = false;
		var win = (parseInt($(".couten").css("width"))) - 60;
		$(".sure_code").css("height", $(document).height());
		$(".couten").css("height", $(document).height());
		$(".backward").css("height", $(document).height());
		// 点击取消的时候关闭模态层
		$(".btn_cancel").click(function(){
			$(".sure_code").css("display", "none");
			$.post("{:U('deleteCancelCode')}",{'id':$("#input_code_id").val()},function(result){
				if (result.status == 1) {
					$("#input_code_id").val('');
				}
			},'json');
		});
		$(".btn_sure").click(function(){
			var user_code_id = $("#input_code_id").val();
			if (user_code_id > 0) {
				location.href = "/Home/pay/surecode/user_code_id/"+ user_code_id+".html";
			}
		});

		//增加红包
		var num = 0;		
		var add = function() {
			var hb = parseInt(Math.random() * (3 - 1) + 1);
			var Wh = parseInt(Math.random() * (70 - 30) + 20);
			var Left = parseInt(Math.random() * (win - 0) + 0);
			var rot = (parseInt(Math.random() * (45 - (-45)) - 45)) + "deg";
			num++;
			$(".couten").append("<li class='li" + num + "' ><a href='javascript:;'><img src='__IMG__/hb_" + hb + ".png'></a></li>");
			$(".li" + num).css({
				"left": Left,
			});
			$(".li" + num + " a img").css({
				"width": Wh,
				"transform": "rotate(" + rot + ")",
				"-webkit-transform": "rotate(" + rot + ")",
				"-ms-transform": "rotate(" + rot + ")", /* Internet Explorer */
				"-moz-transform": "rotate(" + rot + ")", /* Firefox */
				"-webkit-transform": "rotate(" + rot + ")",/* Safari 和 Chrome */
				"-o-transform": "rotate(" + rot + ")" /* Opera */
			});	
			$(".li" + num).animate({'top':$(window).height()+20},5000,function(){
				//删掉已经显示的红包
				this.remove()
			});
			//点击红包的时候弹出模态层
			$(".li" + num).click(function(){
				generateCode();
			});
			setTimeout(add,200)
		}	
		
		var numz = 1;
		backward();
		//倒数计时
		function backward(){
			numz--;
			if(numz>0){
				$(".backward span").html(numz);
				setTimeout(backward,1000)
			}else{
				add();
				$(".backward").remove();
			}
		}
		function generateCode(){
			$.post("{:U('ajaxCheckCode')}",{'snatch_id':snatch_id},function(result){
				console.log(result);
				if (result.status == 1) {
					$(".sure_code .code").empty().text(result.code);
					$(".sure_code").css("display", "block")
					$("#input_code_id").val(result.user_code_id);
				}else{
					mui.confirm(result.info,"提示",['确定'],function(e){
						if (result.url !== '') {
							location.href=result.url;
						}
					})
				}
			},'json');

		}
	})
</script>
</block>
